<?php
namespace app\index\service;

use app\index\model\Menu as MenuModel;

trait Menu
{
    protected static function formatMenuToTree($menu_list, $pid = 0)
    {
        $tree = [];
        foreach ($menu_list as $menu) {
            if ($menu['pid'] == $pid) {
                $menu['child'] = self::formatMenuToTree($menu_list, $menu['id']);
                $tree[] = $menu;
            }
        }
        return $tree;
    }

    public static function getList() {
        $menuList = MenuModel::select();
        $menu = self::formatMenuToTree($menuList, 0);
        return $menu;
    }
}